package com.mkh.st.common.core.exception;

import com.mkh.st.common.core.constant.CommonConstants;
import com.mkh.st.common.core.constant.enums.ResultCodeEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 通用异常
 *
 * @author wh_king 2020-11-7
 */
@Data
@Slf4j
public class BizException extends RuntimeException {

    private static final long serialVersionUID = 1L;
    private int code;
    private String message;
    private ResultCodeEnum resultCodeEnum;

    public BizException(int code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }
	public BizException(String message) {
		super(message);
		this.code = CommonConstants.FAIL;
		this.message = message;
	}
    public BizException(ResultCodeEnum resultCodeEnum) {
        super(resultCodeEnum.getMsg());
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMsg();
        this.resultCodeEnum = resultCodeEnum;
    }

    public BizException(int code, String message, Throwable throwable) {
        super(message, throwable);
        this.code = code;
        this.message = message;
    }

    public BizException(ResultCodeEnum resultCodeEnum, Throwable throwable) {
        super(resultCodeEnum.getMsg(), throwable);
        this.resultCodeEnum = resultCodeEnum;
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMsg();
    }

    /**
     * @return void
     * @description 重写Throwable中printStackTrace方法，打印异常信息
     */
    @Override
    public void printStackTrace() {
        if (resultCodeEnum != null) {
            log.info("异常代码: {}, 异常信息: {}", resultCodeEnum.getCode(), resultCodeEnum.getMsg());
            return;
        }
        log.info("异常代码: {}, 异常信息: {}", code, message);
    }
}
